package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yklx10/cache"
	"yklx10/models"
	"yklx10/pkg"
)

type WWWController struct {
	beego.Controller
}

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (w *WWWController) Deng() {
	username := w.GetString("username")
	password := w.GetString("password")
	if username == "" || password == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名或者密码不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	var data models.User
	err = data.Dengs(username, &data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名不正确",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	cachekey := "cacheID" + username
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cachekey).Int()
	if cacheCount >= 3 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误3次,请稍后再试",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	if data.Password != pkg.Md5str1(password) {
		cache.Rdb.Incr(cache.Ctx, cachekey)
		if cacheCount == 2 {
			cache.Rdb.Set(cache.Ctx, cachekey, 3, time.Hour*2)
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误三次,请稍后再试",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: data,
	}
	w.ServeJSON()
}

func (w *WWWController) RolequangxbAdd() {
	qid, _ := w.GetInt("qid", 0)
	rid, _ := w.GetInt("rid", 0)
	if qid == 0 || rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID或者权限ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	data := models.Rolequangxb{
		Qid: qid,
		Rid: rid,
	}
	err = data.DataAdd(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return
}
func (w *WWWController) Userrolegxblist() {
	uid, _ := w.GetInt("uid", 0)
	if uid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	var data models.Role
	err = data.Relist(uid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: data,
	}
	w.ServeJSON()
	return

}
func (w *WWWController) Rolequangxblist() {
	rid, _ := w.GetInt("rid", 0)
	if rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	var data models.Quan
	err = data.Quantise(rid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: data,
	}
	w.ServeJSON()
	return

}
func (w *WWWController) ComesAdd() {
	codename := w.GetString("codename")
	if codename == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流名称不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	data := models.Comes{
		Codename: codename,
	}
	err = data.ComesAdds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return
}
func (w *WWWController) OrdersAdd() {
	orderliness := w.GetString("orderliness")
	cid, _ := w.GetInt("cid", 0)
	tid, _ := w.GetInt("tid", 0)
	if orderliness == "" || cid == 0 || tid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流单号,物流ID,类型ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	data := models.Orders{
		Orderliness: orderliness,
		Cid:         cid,
		Tid:         tid,
	}
	err = data.OrdersAdds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return
}
func (w *WWWController) List() {
	var err error
	var list []map[string]interface{}
	cacheKey := "ID"
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cacheKey).Result()
	if cacheCount == "" {
		err = models.DB.Table("orders").
			Joins("  LEFT JOIN comes ON comes.id = orders.cid").
			Joins("LEFT JOIN types ON types.id = orders.tid").
			Select("orders.id,orders.orderliness,comes.codename,types.typename").
			Scan(&list).Error
		cachejson, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, cacheKey, cachejson, time.Hour*2)
	}
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
	return
}
func (w *WWWController) Lists() {
	id, _ := w.GetInt("id", 0)
	var err error
	var list []map[string]interface{}
	cacheKey := "IDs" + strconv.Itoa(id)
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cacheKey).Result()
	if cacheCount == "" {
		err = models.DB.Table("orders").
			Joins("  LEFT JOIN comes ON comes.id = orders.cid").
			Joins("LEFT JOIN types ON types.id = orders.tid").
			Select("orders.id,orders.orderliness,comes.codename,types.typename").
			Where("orders.id=?", id).
			Scan(&list).Error
		cachejson, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, cacheKey, cachejson, time.Hour*2)
	}
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
	return
}
